package com.dituhui.web.saferoute.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import com.dituhui.tool.gis.CoordConverter;
import com.dituhui.tool.gis.Point;
import com.dituhui.tool.io.Constants;
import com.dituhui.tool.io.HttpTool;
import com.dituhui.tool.io.RedisTool;
import com.dituhui.web.saferoute.vo.SafeRoute;
import com.dituhui.web.saferoute.vo.SafeTargetPoint;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 读取地图数据
 * 
 * @author liweigu
 *
 */
public class MapDataReader {
	private static final Logger LOGGER = Logger.getLogger(MapDataReader.class);
	private static String RedisUrl = Constants.getValue("REDIS.HOST", "saferouteWeb.properties");
	private static int RedisPort = Integer.parseInt(Constants.getValue("REDIS.PORT", "saferouteWeb.properties"));
	private static String RedisPwd = Constants.getValue("REDIS.PWD", "saferouteWeb.properties");
	private static final String QueryPATH = "https://cute.dituhui.com/layer/queryData";
	private static final String StylePATH = "https://cute.dituhui.com/layer/data/style/get";
	private static final String ImagePATH = "https://cute.dituhui.com/element/queryImages";
	private static final String RedisKeyPrefix = "cute_dituhui_";

	public static String getParam(String key) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("meinianRoute",
				"layer_id=8834&data_id=8840&layer_type=1&map_id=2585&user_id=859&token=eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJ3d3cuZGl0dWh1aS5jb20iLCJzdWIiOiI4NTkiLCJpYXQiOjE0OTY4MTc2NjV9.Dp_6m8dQx7HNNTWboxie1wIuzdJszCqaLwPFXeri8ljPiw9e1CWpSKqUbGZOYiqnZrz5Zh6_A0CQ9bGVD2NJyg");
		params.put("guanghuaRoute",
				"layer_id=83460&data_id=83578&layer_type=1&map_id=2585&user_id=859&token=eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJ3d3cuZGl0dWh1aS5jb20iLCJzdWIiOiI4NTkiLCJpYXQiOjE1MDAzNzI0MTV9.TkvPUqmgO1Js2rgaIqK8wm51cEx475wEHbMzaPo7TXIg2_kM11qSDCrberYMPdS7dMdsTixEIsHWq7p3x3xYsg");
		params.put("guanghuaSafeArea",
				"layer_id=83459&data_id=83577&layer_type=1&map_id=2585&user_id=859&token=eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJ3d3cuZGl0dWh1aS5jb20iLCJzdWIiOiI4NTkiLCJpYXQiOjE1MDAzNzI0MTV9.TkvPUqmgO1Js2rgaIqK8wm51cEx475wEHbMzaPo7TXIg2_kM11qSDCrberYMPdS7dMdsTixEIsHWq7p3x3xYsg");
		params.put("guanghuaSafeEquip",
				"layer_id=8833&data_id=8839&layer_type=0&map_id=2585&user_id=859&token=eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJ3d3cuZGl0dWh1aS5jb20iLCJzdWIiOiI4NTkiLCJpYXQiOjE1MDAzNzI0MTV9.TkvPUqmgO1Js2rgaIqK8wm51cEx475wEHbMzaPo7TXIg2_kM11qSDCrberYMPdS7dMdsTixEIsHWq7p3x3xYsg");
		params.put("guanghuaTestPoint",
				"layer_id=83483&data_id=83601&layer_type=0&map_id=2585&user_id=859&token=eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJ3d3cuZGl0dWh1aS5jb20iLCJzdWIiOiI4NTkiLCJpYXQiOjE1MDAzNzI0MTV9.TkvPUqmgO1Js2rgaIqK8wm51cEx475wEHbMzaPo7TXIg2_kM11qSDCrberYMPdS7dMdsTixEIsHWq7p3x3xYsg");
		params.put("meinianSafeTarget",
				"layer_id=68307&data_id=68395&layer_type=0&map_id=2585&user_id=859&token=eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJ3d3cuZGl0dWh1aS5jb20iLCJzdWIiOiI4NTkiLCJpYXQiOjE0OTY4MTc2NjV9.Dp_6m8dQx7HNNTWboxie1wIuzdJszCqaLwPFXeri8ljPiw9e1CWpSKqUbGZOYiqnZrz5Zh6_A0CQ9bGVD2NJyg");
		return params.get(key);
	}

	private static String getImageParam(String eleId) {
		return "ele_id=" + eleId
				+ "&ele_type=0&data_id=8839&map_id=2585&user_id=859&token=eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJ3d3cuZGl0dWh1aS5jb20iLCJzdWIiOiI4NTkiLCJpYXQiOjE1MDE0MDUyOTh9.kVB1mRp0VwlzwAxNjddByySHXwM9IBULHQy3V4JaCeXGb-J9pAIIVD1Y7IMOGr447pkG3vV1z3314mUfDjb0lg";
	}

	public static List<SafeRoute> getSafeRoutes(String key) {
		List<SafeRoute> safeRoutes = new ArrayList<SafeRoute>();

		String param = getParam(key);
		try {
			String path;
			if ("meinianRoute".equalsIgnoreCase(key)) {
				path = QueryPATH;
			} else {
				path = StylePATH;
			}
			String content = HttpTool.getContentByGET(path, param);
			JSONObject contentJsonObject = JSONObject.fromObject(content);
			boolean status = contentJsonObject.getBoolean("status");
			if (status) {
				JSONObject resultJSONObject = contentJsonObject.getJSONObject("result");
				JSONArray datasJSONObject = resultJSONObject.getJSONArray("datas");
				for (int i = 0; i < datasJSONObject.size(); i++) {
					JSONObject dataJSONObject = datasJSONObject.getJSONObject(i);
					String title = dataJSONObject.getString("title");
					JSONObject lineJSONObject = dataJSONObject.getJSONObject("line_geometry");
					JSONArray pointsJSONArray = lineJSONObject.getJSONArray("apex");
					List<Point> points = new ArrayList<Point>();
					for (int j = 0; j < pointsJSONArray.size(); j++) {
						JSONObject pointJSONObject = pointsJSONArray.getJSONObject(j);
						double bd_x = pointJSONObject.getDouble("lng");
						double bd_y = pointJSONObject.getDouble("lat");
						// 转换坐标
						Point bdPoint = new Point(bd_x, bd_y);
						Point gpsPoint = CoordConverter.baiduToLngLat(bdPoint);
						points.add(gpsPoint);
					}
					SafeRoute safeRoute = new SafeRoute();
					safeRoute.setPoints(points);
					safeRoute.setName(title);
					safeRoutes.add(safeRoute);
				}
				RedisTool redisTool = new RedisTool();
				JSONArray jsonArray = JSONArray.fromObject(safeRoutes);
				redisTool.setString(RedisKeyPrefix + key, jsonArray.toString(), RedisUrl, RedisPort, RedisPwd);
			} else {
				LOGGER.warn("查询失败。url = " + path + "?" + param);
				RedisTool redisTool = new RedisTool();
				String value = redisTool.getString(RedisKeyPrefix + key, RedisUrl, RedisPort, RedisPwd);
				if (value != null && value.length() > 0) {
					JSONArray jsonArray = JSONArray.fromObject(value);
					safeRoutes = new ArrayList<SafeRoute>();
					for (int i = 0; i < jsonArray.size(); i++) {
						JSONObject jsonObject = jsonArray.getJSONObject(i);
						SafeRoute safeRoute = new SafeRoute();
						safeRoute.setName(jsonObject.getString("name"));
						safeRoute.setStatus(jsonObject.getString("status"));
						List<Point> points = new ArrayList<Point>();
						JSONArray jsonArray2 = jsonObject.getJSONArray("points");
						for (int j = 0; j < jsonArray2.size(); j++) {
							JSONObject jsonObject2 = jsonArray2.getJSONObject(j);
							double x = jsonObject2.getDouble("x");
							double y = jsonObject2.getDouble("y");
							Point point = new Point(x, y);
							points.add(point);
						}
						safeRoute.setPoints(points);
						safeRoutes.add(safeRoute);
					}
					LOGGER.info("从缓存里读取到了safeRoutes");
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return safeRoutes;
	}

	public static List<SafeTargetPoint> getSafeTargetPoints(String key) {
		List<SafeTargetPoint> safeTargetPoints = new ArrayList<SafeTargetPoint>();

		String param = getParam(key);
		try {
			String path;
			if ("meinianSafeTarget".equalsIgnoreCase(key)) {
				path = QueryPATH;
			} else {
				path = StylePATH;
			}
			String content = HttpTool.getContentByGET(path, param);
			JSONObject contentJsonObject = JSONObject.fromObject(content);
			boolean status = contentJsonObject.getBoolean("status");
			if (status) {
				JSONObject resultJSONObject = contentJsonObject.getJSONObject("result");
				JSONArray datasJSONObject = resultJSONObject.getJSONArray("datas");
				for (int i = 0; i < datasJSONObject.size(); i++) {
					JSONObject dataJSONObject = datasJSONObject.getJSONObject(i);
					String id = dataJSONObject.getString("marker_id");
					String title = dataJSONObject.getString("title");
					String desc = dataJSONObject.getString("description");
					double bd_x = dataJSONObject.getDouble("bd_x");
					double bd_y = dataJSONObject.getDouble("bd_y");
					// 转换坐标
					Point bdPoint = new Point(bd_x, bd_y);
					Point gpsPoint = CoordConverter.baiduToLngLat(bdPoint);
					SafeTargetPoint safeTargetPoint = new SafeTargetPoint();
					safeTargetPoint.setId(id);
					safeTargetPoint.setX(gpsPoint.getX());
					safeTargetPoint.setY(gpsPoint.getY());
					safeTargetPoint.setName(title);
					safeTargetPoint.setDesc(desc);
					List<String> images = getImages(id);
					safeTargetPoint.setImages(images);
					safeTargetPoints.add(safeTargetPoint);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return safeTargetPoints;
	}

	public static List<String> getImages(String eleId) {
		List<String> images = new ArrayList<String>();

		String param = getImageParam(eleId);
		try {
			String path = ImagePATH;
			String content = HttpTool.getContentByGET(path, param);
			JSONObject contentJsonObject = JSONObject.fromObject(content);
			boolean status = contentJsonObject.getBoolean("status");
			if (status) {
				JSONArray resultJSONObject = contentJsonObject.getJSONArray("result");
				for (int i = 0; i < resultJSONObject.size(); i++) {
					JSONObject dataJSONObject = resultJSONObject.getJSONObject(i);
					String imageUrl = dataJSONObject.getString("image_url");
					images.add(imageUrl);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return images;
	}
}
